package com.zxl.yjb.fragment.my;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.my.MyWalletActivity;
import com.zxl.yjb.adapter.MyWalletListAdapter;
import com.zxl.yjb.base.LazyBaseFragment;
import com.zxl.yjb.bean.MyWalletBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：songs on 2018/5/14 14:28
 * 邮箱：xjs250@163.com
 * 简介：
 */
public class MyWalletDetailFragment extends LazyBaseFragment implements OnRefreshLoadMoreListener {

    private RecyclerView recyclerView;
    private MyWalletListAdapter detailAdapter;
    private String type;
    private SmartRefreshLayout refreshLayout;
    private int currentPager = 1;
    private int pageSize = 20;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_wallet;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.recycler);
        refreshLayout = view.findViewById(R.id.refresh_layout);
    }

    @Override
    protected void main() {
        super.main();
        type = getArguments().getString("type");
        initAdapter();

    }

    private void initAdapter() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置收入明细list
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        detailAdapter = new MyWalletListAdapter(R.layout.item_mywallet,type);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(detailAdapter);
        detailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        refreshLayout.setOnRefreshLoadMoreListener(this);
    }

    @Override
    protected void lazyFetchData() {
        super.lazyFetchData();
        loadList(false);
    }

    private void loadList(boolean isRefresh) {
        if ("income".equals(type)) {
            getIncomeDetails(isRefresh);
        }else {
            getWithdrawList(isRefresh);
        }
    }


    /**
     * 获取收入明细
     * @param isRefresh 是否为刷新
     */
    private void getIncomeDetails(final boolean isRefresh) {
        Map<String, String> map = new HashMap<>();
        map.put("page", currentPager+"");
        map.put("count", pageSize+"");
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<List<MyWalletBean>> listener = new ObserverOnNextListener<List<MyWalletBean>>() {
            @Override
            public void onNext(List<MyWalletBean> detailBeanList) {
                currentPager++;
                if (isRefresh) {
                    detailAdapter.setNewData(detailBeanList);
                    refreshLayout.finishRefresh();
                }else {
                    detailAdapter.addData(detailBeanList);
                    refreshLayout.finishLoadMore();
                    if (detailBeanList.size()<pageSize){
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.incomeDetails(new ProgressObserver<List<MyWalletBean>>(listener,mContext){
            @Override
            public void showDialog() {
            }
        },map);
    }

    /**
     * 获取提现记录
     * @param isRefresh 是否为刷新
     */
    private void getWithdrawList(final boolean isRefresh) {
        Map<String, String> map = new HashMap<>();
        map.put("page", currentPager+"");
        map.put("count", pageSize+"");
        DataManager dataManager = new DataManager();
        ObserverOnNextListener<List<MyWalletBean>> listener = new ObserverOnNextListener<List<MyWalletBean>>() {
            @Override
            public void onNext(List<MyWalletBean> detailBeanList) {
                currentPager++;
                if (isRefresh) {
                    detailAdapter.setNewData(detailBeanList);
                    refreshLayout.finishRefresh();
                }else {
                    detailAdapter.addData(detailBeanList);
                    refreshLayout.finishLoadMore();
                    if (detailBeanList.size()<pageSize){
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.withdrawList(new ProgressObserver<List<MyWalletBean>>(listener,mContext){
            @Override
            public void showDialog() {
            }
        },map);
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(5000);
        loadList(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(5000);
        ((MyWalletActivity)getActivity()).refreshData();
        currentPager=1;
        loadList(true);
        refreshLayout.setNoMoreData(false);
    }
}
